home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / newsgroups / misc.19981211-19990422 / 000396_news@watsun.cc.columbia.edu _Mon Mar 22 11:16:00 1999.msg < prev    next >
Internet Message Format  |  2020-01-01  |  6KB

  1. Return-Path: <news@watsun.cc.columbia.edu>
  2. Received: from newsmaster.cc.columbia.edu (newsmaster.cc.columbia.edu [128.59.59.30])
  3.     by watsun.cc.columbia.edu (8.8.5/8.8.5) with ESMTP id LAA14226
  4.     for <kermit.misc@watsun.cc.columbia.edu>; Mon, 22 Mar 1999 11:16:00 -0500 (EST)
  5. Received: (from news@localhost)
  6.     by newsmaster.cc.columbia.edu (8.8.5/8.8.5) id LAA02385
  7.     for kermit.misc@watsun.cc.columbia.edu; Mon, 22 Mar 1999 11:10:28 -0500 (EST)
  8. X-Authentication-Warning: newsmaster.cc.columbia.edu: news set sender to <news> using -f
  9. From: dn5006@my-dejanews.com
  10. Subject: Re: Associative Array in Kermit 95
  11. Date: Mon, 22 Mar 1999 16:02:44 GMT
  12. Organization: Deja News - The Leader in Internet Discussion
  13. Message-ID: <7d5pit$on3$1@nnrp1.dejanews.com>
  14. To: kermit.misc@watsun.cc.columbia.edu
  15.  
  16. In article <36F5ECE5.5E8A07D1@value.net>,
  17.   Mark Sapiro <msapiro@value.net> wrote:
  18. > Frank da Cruz wrote:
  19. > >>
  20. > In article <7cttsc$47f$1@nnrp1.dejanews.com>,  <dn5006@my-dejanews.com> wrote:
  21. >
  22. > : Associative array is a very useful feature in scripting language, it enables
  23. > : Perl, Tcl etc. the implementation of comlex data structures.  The following
  24. > : script demonstrates that associative array can also be crafted in Kermit
  25. > : 95. The script counts the unique words in a regular english, french, german,
  26. > : etc. text file.
  27. > :
  28. > : open read testfile.txt
  29. > : if fail end 1 Can't open testfile.txt
  30. > : assign \%n 0                                  ; init register
  31. > : while true {
  32. > :     read \%l                                  ; read each line
  33. > :     if fail break                             ; until the end of file
  34. > :     while > \flength(\%l) 0 {
  35. > :         assign \%w \fbreak(\%l,{ })           ; split on space
  36. > :         xif defined \m(\%w) {                 ; word already seen?
  37. > :             _assign \%w \feval(\m(\%w) + 1)   ; incr count this word
  38. > :         } else {
  39. > :             _assign \%w 1                     ; init count this word
  40. > :             increment \%n                     ; next register
  41. > :             _assign \%n \%w                   ; register this word
  42. > :         }
  43. > :         assign \%l \fltrim(\fright(\%l,-
  44. > :         \feval(\flength(\%l)-\flength(\%w)))) ; shift to next word
  45. > :     }
  46. > : }
  47. > : for \%k 1 \%n 1 {
  48. > :       assign \%w \m(\%k)                      ; get word from register
  49. > :       echo <\m(\%w)> \%w                      ; display occurences
  50. > : }
  51. > :
  52. > : This approach avoids the use of array which has to be declared in advance.
  53. > : The script does not take into account the non alphanumeric characters.
  54. > :
  55. > : Dat Nguyen
  56. > : Airline Telecommunications and Information Services
  57. > : 770 Sherbrooke West
  58. > : Montreal, Quebec
  59. > : Canada H3A 1G1
  60. > : Email dat.nguyen&sita.int
  61. > :
  62. > Excellent!  I've had associative arrays on my list for quite a while, but
  63. > the list so long and time so short.  I've reformatted your script to fit
  64. > in 80 columns.
  65. >
  66. > We plan to add a script library to the Kermit website -- this one will
  67. > certainly go into it.  Other submissions are welcome too; send them in!
  68. > (Be sure to document the Kermit program and version and other relevant
  69. > info.)
  70. >
  71. > A quick glance shows this script doesn't use any new (post-C-Kermit-6.0)
  72. > features, some of which would make it simpler and faster, for example
  73. > the new \fword() and \fsplit() functions for extracting words from strings,
  74. > with specified break masks (e.g. to make sure punctuation does not count
  75. > as part of word (e.g. "thing" and "thing.").  Also you can convert each
  76. > word to lowercase with \flower() so "Thing", "thing", and "THING" count as
  77. > the same word, etc.
  78. >
  79. > Readers should take special note of the "_assign" verb, which is subtly
  80. > different from "assign" (see p.457 of the manual).
  81. >
  82. > - Frank
  83. > >>
  84. >
  85. > There is a problem with the above approach to implementing associative
  86. > arrays.  Namely, if one of the 'words' used as an index happens to be
  87. > equal to or an initial substring of a pre-defined macro (e.g. "cautious" or
  88. > "robust") or one of the macros that gets defined implicitly when certain
  89. > programming constructs are used (e.g. "break " or "continue"), the
  90. > "xif defined" test gives the "wrong" result and the word doesn't get
  91. > counted (although the macro 'word' does get (re-)defined, most likely
  92. > with an error message from \feval()).
  93. >
  94. > This problem could probably be avoided by prepending some prefix to
  95. > each word before using it as a macro name.
  96. >
  97. > --
  98. > Mark Sapiro <msapiro@value.net>       The highway is for gamblers,
  99. > San Francisco Bay Area, California    better use your sense - B. Dylan
  100. >
  101.  
  102. That's right, and I was about to improve the script as follows (but it still
  103. won't eliminate the collision 100%):
  104.  
  105. open read testfile.txt
  106. if fail end 1 Can't not open testfile.txt
  107. assign \%n 0
  108. while true {
  109.     read \%l
  110.     if fail break
  111.     echo \%l
  112.     while > \flength(\%l) 0 {
  113.         assign \%w \fbreak(\%l,{ })
  114.         xif defined \m(word_count[\%w]) {
  115.             _assign word_count[\%w] \feval(\m(word_count[\%w])+1)
  116.         } else {
  117.             _assign word_count[\%w] 1
  118.             increment \%n
  119.             _assign word_reg[\%n] \%w
  120.         }
  121.         assign \%l \fltrim(\fright(\%l,-
  122.         \feval(\flength(\%l)-\flength(\%w))))
  123.     }
  124. }
  125.  
  126. for \%k 1 \%n 1 {
  127.     assign \%w \m(word_reg[\%k])
  128.     echo <\m(word_count[\%w])> \%w
  129. }
  130.  
  131. Dat Nguyen
  132. Airline Telecommunications and Information Services
  133. 770 Sherbrooke West
  134. Montreal, Quebec
  135. Canada H3A 1G1
  136. Email dat.nguyen&sita.int
  137.  
  138. -----------== Posted via Deja News, The Discussion Network ==----------
  139. http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own